package com.mathplayer.proc;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Node;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.mathplayer.player.MathManager;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class MathProc implements EntryPoint {
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		initJavaScriptAPI();
		process();
	}
	
	private static void process(){
		NodeList<Element> nodes =  RootPanel.getBodyElement().getElementsByTagName("script");
		
		while (nodes.getLength() > 0){
			int n = 0;
			if (((Element)nodes.getItem(n)).hasAttribute("type")  &&  ((Element)nodes.getItem(n)).getAttribute("type").toLowerCase().equals("text/mml")){
				String src = ((Element)nodes.getItem(n)).getInnerHTML();
				FlowPanel mathPanel = new FlowPanel();
				mathPanel.setStyleName("math-player-item");
				nodes.getItem(n).getParentNode().replaceChild(mathPanel.getElement(), nodes.getItem(n));
				MathManager mm = new MathManager();
				mm.createMath(src, mathPanel);
			}
		}
	}

	private native void initJavaScriptAPI()/*-{
		$wnd.mathPlayerProcess = function(){
			return @com.mathplayer.proc.MathProc::process()();
		}
	}-*/;

}
